﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;
using Ciripescu_WebRole.Properties;

namespace Ciripescu_WebRole
{
    /// <summary>
    /// Toti urmaritorii unui user.(dat in query string)
    /// </summary>
    public partial class allUrmaritori : BasePageAnonAndLoggedIn
    {

        protected void RefreshUrmaritori(int page)
        {
            string forUserName = String.Empty;

            if (Request.QueryString["userName"] != null)
                  forUserName = Request.QueryString["userName"];

            if (string.IsNullOrEmpty(forUserName))
            {
                errors.InnerHtml = Resources.ErrInvalidUser;
                return;
            }

            UserManager um = new UserManager();
            User user = um.GetItem(forUserName);
            if (user == null)
            {
                errors.InnerHtml = Resources.ErrUserNotExists;
                return;
            }
            
            lblNrUrmaritori.Text = user.NrUrmaritori.ToString() + " " + Resources.Urmaritori;

            UrmarireManager urmManag = new UrmarireManager();
            List<User> usersList = urmManag.GetFollowersByUser(forUserName, page);

            if (page == 1 &&
               usersList.Count < UrmarireManager.m_nbOfUsersPerPage) //prima pagina no next daca avem prea putini useri
                btnNext.Visible = false;

            if (page > 1 && 
                usersList.Count == 0) //cicle to the first page
                //Response.Redirect("/allUrmaritori.aspx?page=1");  //revenim la prima pagina cand sau terminat userii
                Response.Redirect("/allUrmaritori/" +forUserName + "/1/");  //revenim la prima pagina cand sau terminat userii
            else//populare
                {
                    rptUrmaritori.DataSource = usersList;
                    rptUrmaritori.DataBind();
                }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
                Response.Redirect("/");
            if(Request.QueryString["page"] == null)
                Response.Redirect("/");
            if(Request.QueryString["userName"] == null)
                Response.Redirect("/");
            
            int page = 1;
            try
            {
                page   = Convert.ToInt32(Request.QueryString["page"]);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(EventLogName.Warning, ex.ToString());
                page = 1;
            }

            if (!IsPostBack)
            {
                RefreshUrmaritori(page);
            }
        }

        protected void rptUrmaritori_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ImageButton b = (ImageButton)e.CommandSource;

            UrmarireManager urmManag = new UrmarireManager();
            Urmarire urm             = new Urmarire((string)Session["UserName"], b.CommandArgument);
            urmManag.Insert(urm);
            
            Label lblFollowed     = (Label)e.Item.FindControl("lblAlreadyFollowed");
            lblFollowed.Visible   = true;
            b.Visible = b.Enabled = false; 
        }

        protected void rptUrmaritori_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string currentUser = (string)Session["UserName"]; // userul logat
            User usr = (User)e.Item.DataItem;
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            UserManager uManag = new UserManager();
            ImageButton btnFollow   = (ImageButton)e.Item.FindControl("btnFollow");
            Label lblFollowed  = (Label)e.Item.FindControl("lblAlreadyFollowed");

            if (uManag.Follows(currentUser, usr.GetUserName()))
            {
                btnFollow.Visible   = false;
                lblFollowed.Visible = true;
            }
            else
            {
                btnFollow.Visible   = true;
                lblFollowed.Visible = false;
            }

            if (currentUser == usr.GetUserName())
            {
                btnFollow.Visible = false;
                lblFollowed.Visible = false;
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["page"] == null)
                Response.Redirect("/");
            else
            {
                int page = 1;
                try
                {
                    page = Convert.ToInt32(Request.QueryString["page"]);
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(EventLogName.Warning, ex.ToString());
                    page = 1;
                }
                page++;//increase page number
                
                //Response.Redirect("/allUrmaritori.aspx?page=" + page.ToString());
                string forUserName = String.Empty;

                if (Request.QueryString["UserName"] != null)
                    forUserName = Request.QueryString["UserName"];

                if (string.IsNullOrEmpty(forUserName))
                {
                    errors.InnerHtml = Resources.ErrInvalidUser;
                    return;
                }
                Response.Redirect("/allUrmaritori/" + forUserName + "/" + page.ToString() + "/");
            }
        }
    }
}
